#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Time          : 2024/4/30 17:39
# File          : init_sql_alchemy.py
# @Author       : MingTai
# @Department   : automation_qa
from flask import Flask
from flask_sqlalchemy import SQLAlchemy

from app.business.config import config_base

flask_app = Flask(__name__)
db = SQLAlchemy()

MYSQL_HOST_2 = "127.0.0.1"
MYSQL_PORT_2 = 3306
MYSQL_USER_2 = "root"
MYSQL_PASSWORD_2 = "123456"
MYSQL_DATABASE_2 = "flask_demo"


def init_db(flask_app):
    # # 为了解决控制台的提示
    # app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
    pass


def init_db(flask_app):
    # url的格式为：数据库的协议：//用户名：密码@ip地址：端口号（默认可以不写）/数据库名
    flask_app.config[
        "SQLALCHEMY_DATABASE_URI"] = config_base.local_host_mysql

    # # 为了解决控制台的提示
    # app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False

    # 多数据库配置
    flask_app.config["SQLALCHEMY_BINDS"] = {
        "db_01": config_base.local_host_mysql,
        "db_02": config_base.local_host_mysql,
        "db_03": config_base.local_host_mysql
    }

    # 设置每次请求结束后会自动提交数据库的改动
    flask_app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
    flask_app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True

    # 查询时显示原始SQL语句
    flask_app.config['SQLALCHEMY_ECHO'] = False

    # 创建数据库的操作对象

    db.init_app(app=flask_app)
    return db


def creat_app():
    init_db(flask_app)
    return flask_app
